package com.java.service.impl;

import com.java.mapper.StudentFunctionMapper;
import com.java.mapper.TeacherFunctionMapper;
import com.java.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * description:描述
 * author:作者
 * time:2020/11/23 23:17
 */
@Service
public class StudentFunctionServiceImpl implements com.java.service.StudentFunctionService {

    @Autowired
    private StudentFunctionMapper studentFunctionMapper;
    @Autowired
    private TeacherFunctionMapper teacherFunctionMapper;

    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
    /**
     * 通过微信号查看学生个人信息
     * @param Wxid
     * @return
     */
    @Override
    public Student_info findStudentInfoByWxid(String Wxid){
        return studentFunctionMapper.selectStudentInfoByWxid(Wxid);
    }

    public boolean updateStudentInfo(Student_info student_info){


        return studentFunctionMapper.updateStudentInfo(student_info)==1;
    }

    public List<CourseList> findStudentCourseByWxid(String Wxid){



        return studentFunctionMapper.selectStudentCourse(Wxid);
    }

    @Override
    public List<CourseList> findAllCourse() {
        return studentFunctionMapper.selectAllCourse();
    }

    @Override
    public Course_detail findCourseDetailByWxid(String courseId) {

        return studentFunctionMapper.selectCourseDetail(courseId);
    }

    @Override
    public boolean addStudentCourse(String studentWxid, String courseId) {

        List<CourseList> list = studentFunctionMapper.selectStudentCourse(studentWxid);
        if(list.size()>0){
            System.out.println("1"+list.isEmpty());
            try {

                for(CourseList entity: list){
                    if(courseId.equals(entity.getCourseId())){
                        return false;
                    }
                }
            }catch(Exception e){
                e.printStackTrace();
            }
        }
        String uuid=studentFunctionMapper.getUuidByWxid(studentWxid);
        return studentFunctionMapper.addStudentCourseInfo(uuid,courseId)==1;
    }

    @Override
    public boolean deleteStudentCourse(String studentWxid, String courseId) {
        String uuid=studentFunctionMapper.getUuidByWxid(studentWxid);
        System.out.println("uuid is:"+uuid);
        return studentFunctionMapper.deleteStudentCourseInfo(uuid,courseId)==1;
    }

    @Override
    public int checkSuccess(CheckInfo checkClassInfo) {


        String courseId = checkClassInfo.getCourseId();
        //获取当前课程周次
        Course_detail detail = studentFunctionMapper.selectCourseDetail(courseId);
        int flag=detail.getCheckFlag();
        if(flag==0)return 2;//不在签到时间内2

        String uuid=studentFunctionMapper.getUuidByWxid(checkClassInfo.getStudentId());
        checkClassInfo.setStudentId(uuid);//获取学生Uuid
        int now=0;
        try {
            now = detail.getCourseNow();
        }catch (Exception e){
            return 3;//周次异常3
        }
        checkClassInfo.setCourseNum(String.valueOf(now));
        int isExist = studentFunctionMapper.getCheckInfo(checkClassInfo);
        if(isExist>0){
            return 4;//已签到4
        }
        checkClassInfo.setCheckTime(df.format(new Date()));

        String  checkUuid = UUID.randomUUID().toString().replace("-","").toLowerCase();
        checkClassInfo.setUuid(checkUuid);

        return studentFunctionMapper.checkSuccess(checkClassInfo);//签到成功1，数据库写入异常0
    }

}
